var login = {
    URL: {
        checkLoginUrl: "../login/checkLogin",
        createKeyUrl: "../login/createKey",
        loginUrl: "../login/adminLogin",
    },
    detail: {
        init: function () {
            //登录检测
            $.ajax({
                url: login.URL.checkLoginUrl,
                type: "post",
                data: {},
                dataType: 'json',
                async: true,
                success: function (data) {
                    if (!data.hasError) {
                        window.top.location.href = data.errUrl;
                        return false;
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                }
            });


            $("#login_submit").click(function () {
                layer.load();
                //alert($("[name='password']").val());
                $.ajax({
                    url: login.URL.createKeyUrl,
                    data: {},
                    dataType: "json",
                    type: "post",
                    async: true,
                    success: function (data) {
                        if (!data.hasError) {
                            var publicKey = RSAUtils.getKeyPair(data.object.Exponent, '', data.object.Modulus);
                            //颠倒密码的顺序，要不然后解密后会发现密码顺序是反的
                            var reversedPwd = $("[name='psd']").val().split("").reverse().join("");
                            //对密码进行加密传输
                            $("[name='password']").val(RSAUtils.encryptedString(publicKey, reversedPwd));
                            $("#subf").submit();
                        } else {
                            layer.closeAll('loading');
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        layer.closeAll('loading');
                        layer.msg("系统繁忙，请稍后" + XMLHttpRequest.status + "," + XMLHttpRequest.readyState + "," + textStatus, function(){});
                    }
                });
            });


            function check() {
                var account = $("[name='account']").val();
                if (__JsUtil.isNullOrEmpty(account)) {
                    layer.closeAll('loading');
                    layer.msg("请输入用户名", function(){});
                    return false;
                }
                var psd = $("[name='psd']").val();
                if (__JsUtil.isNullOrEmpty(psd)) {
                    layer.closeAll('loading');
                    layer.msg("请输入密码", function(){});
                    return false;
                }
                return true;
            }

            $("#subf").ajaxForm({
                //定义返回JSON数据，还包括xml和script格式
                //url: login.URL.loginUrl,
                dataType: 'json',
                beforeSend: function () {
                    //表单提交前做表单验证
                    return check();
                },
                success: function (data) {
                    if (!data.hasError) {
                        window.top.location.href = data.errUrl;
                        return false;
                    } else {
                        layer.closeAll('loading');
                        layer.msg(data.error);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    layer.closeAll('loading');
                    layer.msg("系统繁忙，请稍后" + XMLHttpRequest.status + "," + XMLHttpRequest.readyState + "," + textStatus, function(){});
                }
            });
        },
    }
}